package com.dlwx.parttimetalent.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.OrderRecBean;
import com.dlwx.parttimetalent.inter.CheckChangeInter;
import com.dlwx.parttimetalent.views.CustomExpandableListview;

import java.util.List;

/**
 * Created by Administrator on 2017/11/24.
 */

public class OrderRecExpandAdapter extends BaseExpandableListAdapter {
    private final LayoutInflater mInflater;
    public List<OrderRecBean.BodyBean> group ;
    private Context context;
    private CheckChangeInter inter;
    private int hyyrPosition=0;

    public OrderRecExpandAdapter( List<OrderRecBean.BodyBean> group, Context context, CheckChangeInter inter) {
        this.group = group;
        this.context = context;
        this.inter=inter;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<OrderRecBean.BodyBean.ChildBeanX> childList=null;
        if(group.size()>groupPosition){
            childList = group.get(groupPosition).getChild();
        }
        if(childList==null){
            return 0;
        }
//        if(group.get(groupPosition).getLabelclassid()==15){
//            return 1;
//        }
        return childList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<OrderRecBean.BodyBean.ChildBeanX> childList = group.get(groupPosition).getChild();
        if(childList==null){
            return null;
        }
        return childList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder=new ViewHolder();
            convertView = mInflater.inflate(R.layout.expand_group, null);
            viewHolder.tvTitle= convertView.findViewById(R.id.tv_title);
            viewHolder.swStatus1=convertView.findViewById(R.id.sw_group);
            viewHolder.imageArrow=convertView.findViewById(R.id.image_arrow);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        if(isExpanded){
            viewHolder.imageArrow.setBackgroundResource(R.mipmap.arrow_down);
        }else{
            viewHolder.imageArrow.setBackgroundResource(R.mipmap.arrow_top);
        }
        viewHolder.tvTitle.setText(group.get(groupPosition).getClassname());
        if(groupPosition==(group.size()-1)){
            viewHolder.imageArrow.setVisibility(View.GONE);
            viewHolder.swStatus1.setVisibility(View.VISIBLE);
            viewHolder.swStatus1.setOnCheckedChangeListener(null);
           if(group.get(groupPosition).getStart()==1){
               viewHolder.swStatus1.setChecked(true);
           }else{
               viewHolder.swStatus1.setChecked(false);
           }
        }else{
            viewHolder.imageArrow.setVisibility(View.VISIBLE);
            viewHolder.swStatus1.setVisibility(View.INVISIBLE);
        }
        viewHolder.swStatus1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                inter.groupChange(group.get(groupPosition).getLabelclassid(),isChecked);
            }
        });
        return convertView;
    }
    @SuppressLint("WrongViewCast")
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder=new ChildViewHolder();
            convertView = mInflater.inflate(R.layout.expand_child1, null);
            viewHolder.tvContent=convertView.findViewById(R.id.tv_content);
            viewHolder.swStatus2=convertView.findViewById(R.id.sw_status);
            viewHolder.expandListview=convertView.findViewById(R.id.expand_listview);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ChildViewHolder)convertView.getTag();
        }
        OrderRecBean.BodyBean groupItem = group.get(groupPosition);
        if(groupItem!=null){
           if(groupItem.getLabelclassid()==15) {
               hyyrPosition=groupPosition;
               viewHolder.expandListview.setVisibility(View.VISIBLE);
               viewHolder.swStatus2.setVisibility(View.GONE);
               viewHolder.tvContent.setVisibility(View.GONE);
               bindExpandList(groupItem, viewHolder.expandListview);
           }else{
               viewHolder.expandListview.setVisibility(View.GONE);
               viewHolder.swStatus2.setVisibility(View.VISIBLE);
               viewHolder.tvContent.setVisibility(View.VISIBLE);
               List<OrderRecBean.BodyBean.ChildBeanX> childList = group.get(groupPosition).getChild();
               bindList(childList,viewHolder,groupPosition,childPosition);
           }
        }
        return convertView;
    }

    private void bindList(final List<OrderRecBean.BodyBean.ChildBeanX> childList, ChildViewHolder viewHolder, final int groupPosition, final int childPosition) {
        if(childList!=null&&!childList.isEmpty()){
            OrderRecBean.BodyBean.ChildBeanX childBean = childList.get(childPosition);
            viewHolder.tvContent.setText(childBean.getLabelname());
            viewHolder.swStatus2.setOnCheckedChangeListener(null);
            if(childBean.getStart()==1){
                viewHolder.swStatus2.setChecked(true);
            }else{
                viewHolder.swStatus2.setChecked(false);
            }
        }
        viewHolder.swStatus2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                inter.childChange(childList.get(childPosition).getLabelid(),childPosition,isChecked);
            }
        });
    }

    private void bindExpandList(OrderRecBean.BodyBean groupItem, CustomExpandableListview expandListview) {
        OrderRecHyyrExpandAdapter mAdapter = new OrderRecHyyrExpandAdapter(groupItem.getChild(), context, new CheckChangeInter() {
            @Override
            public void groupChange(int position, boolean status) {}
            @Override
            public void childChange(int groupPosition, int childPosition, boolean status) {
                int state = 0;
                if (status) {
                    state = 1;
                } else {
                    state = 2;
                }
                inter.childChange(groupPosition,0,status);
            }
        });
        expandListview.setGroupIndicator(null);
        expandListview.setAdapter(mAdapter);
        int width = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
        expandListview.setIndicatorBounds(width - 100, width - 10);
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolder
    {
        TextView tvTitle;
        Switch swStatus1,swStatus2;
        ImageView imageArrow;
        boolean isOpen;

    }
    private static class ChildViewHolder{
        TextView tvContent;
        Switch swStatus2;
        CustomExpandableListview expandListview;

    }
}
